//
//  ViewController.m
//  PayDemo
//
//  Created by RaoQiangHua on 15/8/26.
//  Copyright (c) 2015年 RaoQiangHua. All rights reserved.
//

#import "ViewController.h"
#import <BmobPay/BmobPay.h>

@interface ViewController ()<BmobPayDelegate>

@property (weak, nonatomic) IBOutlet UITextField *PayNumber;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (IBAction)CommitPay:(UIButton *)sender {
    BmobPay* bPay = [[BmobPay alloc] init];
    bPay.delegate = self;
    //设置商品的价格、商品名、商品描述
    [bPay setPrice:@12];
    [bPay setProductName:@"包子"];
    [bPay setBody:@"好吃的包子"];
    
    //appScheme为创建项目中的URL Scheme中添加的标识
    NSString* appScheme = @"PayDemo";
    [bPay setAppScheme:appScheme];
    
    [bPay payInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
        if(isSuccessful){
            NSLog(@"支付成功！");
        }else{
            NSLog(@"支付失败！");
        }
    }];
}

/*查询订单*/
- (IBAction)checkOrder:(UIButton *)sender {
    BmobPay* bPay = [[BmobPay alloc] init];
    [bPay setTradeNo:@"23111"];
    [bPay queryInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
        if(isSuccessful){
            NSLog(@"订单状态为：%@",[bPay tradeStatus]);
        }
        else{
            NSLog(@"%@",[error description]);
        }
    }];
    
}

-(void)paySuccess{
    UIAlertView* successView = [[UIAlertView alloc] initWithTitle:@"支付结果" message:@"支付成功" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
    [successView show];
}

-(void)payFailWithErrorCode:(int)errorCode{
    switch(errorCode) {
        case 6001:
        {
            UIAlertView* alter = [[UIAlertView alloc] initWithTitle:@"支付结果"
                                                            message:@"用户中途取消"
                                                           delegate:nil
                                                  cancelButtonTitle:@"关闭"
                                                  otherButtonTitles:nil];
            [alter show];
        }
            break;
        case 6002:
        {
            UIAlertView* alter = [[UIAlertView alloc] initWithTitle:@"支付结果"
                                                            message:@"网络连接错误"
                                                           delegate:nil
                                                  cancelButtonTitle:@"关闭"
                                                  otherButtonTitles:nil];
            [alter show];
        }
            break;
        case 4000:
        {
            UIAlertView* alter = [[UIAlertView alloc] initWithTitle:@"支付结果"
                                                            message:@"订单支付失败"
                                                           delegate:nil
                                                  cancelButtonTitle:@"关闭"
                                                  otherButtonTitles:nil];
            [alter show];
        }
            break;
    }
}

@end












